import { View, ScrollView } from '@tarojs/components'
import { memo, ReactNode } from 'react'
import './index.scss'

interface BtScrollProps {
  /** 子元素 */
  children: ReactNode
  /** 下拉刷新回调 */
  onRefresh?: () => Promise<void>
  /** 加载更多回调 */
  onLoadMore?: () => Promise<void>
  /** 是否触发下拉刷新 */
  refresherTriggered?: boolean
  /** 自定义类名 */
  className?: string
}

function BtScroll({
  children,
  onRefresh,
  onLoadMore,
  refresherTriggered = false,
  className = ''
}: BtScrollProps) {
  return (
    <ScrollView
      className={`bt-scroll flex-1 overflow-hidden ${className}`}
      scrollY
      scrollWithAnimation
      enhanced
      showScrollbar
      refresherEnabled={!!onRefresh}
      refresherTriggered={refresherTriggered}
      onRefresherRefresh={onRefresh}
      onScrollToLower={onLoadMore}
    >
      <View className='bt-scroll__content'>
        {children}
      </View>
    </ScrollView>
  )
}

export default memo(BtScroll)
